uniform sampler2D jvr_Texture0;
uniform sampler2D jvr_Texture1;
uniform sampler2D jvr_Texture2;
uniform vec4 jvr_Material_Ambient;

uniform bool  jvr_UseClipPlane0;
varying float clipDist0;

varying vec2 texCoord;

uniform float border;
uniform float border2;
uniform float borderSize;
uniform float borderSize2;
varying float height;

vec4 getTerrainFragment()
{
   	vec4 final_color;
   	if(height > (border2+borderSize2))
   	{
   		final_color = texture2D(jvr_Texture2,texCoord);
   	}
   	else 
   	{
	   	if(height > (border2-borderSize2))
   		{
   			vec4 c0 = texture2D(jvr_Texture1, texCoord);
	   		vec4 c1 = texture2D(jvr_Texture2, texCoord);
	   		float fade = smoothstep(1.0,0.0,(height-border2+borderSize2)/(2*borderSize2));
	   		final_color = fade*c0 + (1-fade)*c1;
   		}  	
	   	else if(height>(border+borderSize))
	   	{
	   		//rock
	   		final_color = texture2D(jvr_Texture1, texCoord);
	   	}
	   	else if(height<(border-borderSize))
	   	{
	   		//gras
	   		final_color = texture2D(jvr_Texture0, texCoord);
	   	}
	   	else
	   	{
	   		//smooth from gras to rock
	   		vec4 c0 = texture2D(jvr_Texture0, texCoord);
	   		vec4 c1 = texture2D(jvr_Texture1, texCoord);
	   		float fade = smoothstep(1.0,0.0,(height-border+borderSize)/(2*borderSize));
	   		final_color = fade*c0 + (1-fade)*c1;
	   	}
   	}
   	
   	return final_color;
}

void main (void)
{   	
   	if(jvr_UseClipPlane0)
	{
		if(clipDist0<0.0) discard;
	}

	gl_FragColor = jvr_Material_Ambient * getTerrainFragment();
}
